import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { environment } from '@env/environment';
import { VolunteerService } from '../../volunteer/service/volunteer.service';
import { NzNotificationService } from 'ng-zorro-antd/notification';
import { NzDrawerService } from 'ng-zorro-antd/drawer';
import { SystemDictService } from '../../../system/dict/service/service.service';
import { VolunteerEditComponent } from '../../volunteer/edit/edit.component';
import * as moment from 'moment';
import { BlogService } from '../service/blog.service';
import { BlogEditComponent } from '../edit/edit.component';
@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.less'],
})
export class BlogListComponent implements OnInit {
  searchForm: any = FormGroup;
  dataList: any;

  constructor(
    private fb: FormBuilder,
    private service: BlogService,
    private notification: NzNotificationService,
    private drawerService: NzDrawerService,
    private dictService: SystemDictService,
  ) {}

  ngOnInit(): void {
    this.searchForm = this.fb.group({
      title: null,
      keyword: null,
      createTime: null,
    });
    this.load();
  }

  add() {
    this.drawerService
      .create({
        nzContent: BlogEditComponent,
        nzContentParams: {},
        nzBodyStyle: {
          height: 'calc(100% - 55px)',
          overflow: 'auto',
          'padding-bottom': '53px',
          'padding-top': '0',
        },
        nzTitle: '添加博客',
        nzWidth: 'calc(100vw - 1000px)',
        nzHeight: '100%',
      })
      .afterClose.subscribe((res: any) => {
        this.load();
      });
  }

  edit(record: any) {
    this.drawerService
      .create({
        nzContent: BlogEditComponent,
        nzContentParams: { record },
        nzBodyStyle: {
          height: 'calc(100% - 55px)',
          overflow: 'auto',
          'padding-bottom': '53px',
          'padding-top': '0',
        },
        nzTitle: '编辑博客',
        nzWidth: 'calc(100vw - 1000px)',
        nzHeight: '100%',
      })
      .afterClose.subscribe((res: any) => {
        this.load();
      });
  }

  reset(): void {
    this.searchForm.reset();
    this.load();
  }

  search(): void {
    this.load();
  }

  load(): void {
    const params = {
      ...this.searchForm.value,
    };

    if (this.searchForm.value.createTime !== null && this.searchForm.value.createTime.length > 0) {
      params['createTimeStart'] = moment(this.searchForm.value.createTime[0]).format('YYYY-MM-DD');
      params['createTimeEnd'] = moment(this.searchForm.value.createTime[1]).format('YYYY-MM-DD');
      delete params['createTime'];
    }

    //删除没有的属性和空的
    for (let key in params) {
      if (params.hasOwnProperty(key) && (params[key] == null || params[key] == undefined || params[key] === ' ')) {
        delete params[key];
      }
    }
    this.service.list(params).subscribe((res) => {
      this.dataList = res.data;
    });
  }

  remove(id: string): void {
    this.service.remove(id).subscribe((res) => {
      this.load();
    });
  }
}
